#include "Demo.h"
#include <windowsx.h>
//////////////////////////////////////////////////////////////////////////
// CDemoApp
//////////////////////////////////////////////////////////////////////////
CDemoApp::CDemoApp()
{

}

CDemoApp::~CDemoApp()
{

}

bool CDemoApp::InitInstance()
{
	m_pMainWnd = new CDemoWnd();
	m_pMainWnd->ShowWindow(SW_SHOW);
																   
	return true;
}

//////////////////////////////////////////////////////////////////////////
// CDemoWnd
//////////////////////////////////////////////////////////////////////////
#define MSGHANDLE(nMsg, MsgProc)  {(nMsg), (&CDemoWnd::MsgProc)}

const CDemoWnd::TMSGMAP CDemoWnd::MsgMapTable[] = {
	MSGHANDLE(WM_CREATE,		OnCreate),
	MSGHANDLE(WM_DESTROY,		OnDestroy), 
	MSGHANDLE(WM_COMMAND,		OnCommand),
};

CDemoWnd::CDemoWnd(const TCHAR* szAppName)
{	
	Create(szAppName, WS_OVERLAPPEDWINDOW, 0, 0, 800, 480, NULL);
}

CDemoWnd::~CDemoWnd()
{
}

LRESULT	 CDemoWnd::WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	for (int ii = 0; ii < sizeof(MsgMapTable) / sizeof(*MsgMapTable); ii++)
	{
		if (nMsg == MsgMapTable[ii].m_nMsg)
		{
			(this->*MsgMapTable[ii].m_MsgProc)(hWnd, nMsg, wParam, lParam);
			break;
		}
	}

	return __super::FinalWndProc(hWnd, nMsg, wParam, lParam);
}

LRESULT CDemoWnd::OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	CenterWindow();

	if (_CreateCtrls(m_hWnd, m_hCtrls))
	{
		_InitCtrls(hWnd, m_hCtrls);
	}

	return 0;
}

LRESULT CDemoWnd::OnDestroy(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	_DestroyCtrls(m_hCtrls);

	PostQuitMessage(0);

	return 0;
}

LRESULT CDemoWnd::OnPaint(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hDC = BeginPaint(hWnd, &ps);
	
	EndPaint(hWnd, &ps);

	return 1;
}

LRESULT CDemoWnd::OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	


 	return 1;
}

bool CDemoWnd::_CreateCtrls(HWND hWnd, HWND* hCtrls)
{
	RECT rtClient;
	GetClientRect(m_hWnd, &rtClient);
	InflateRect(&rtClient, -20, -20);
	UINT nClientW			= rtClient.right - rtClient.left;
	const UINT nInterval	= 30;
	const UINT nBtnWidth    = (nClientW - (m_nCtrlCount * nInterval)) / m_nCtrlCount;
	const UINT nBtnHeight	= 50;

	struct SCtrlInfo 
	{
		TCHAR	m_szClsName[64];
		TCHAR	m_szCaption[128];
		DWORD	m_dwStyle;
		UINT	m_nID;
	};
	
	SCtrlInfo CtrlInfoList[m_nCtrlCount] = {
		{_T("Button"),		_T("Overlepped Wnd"),	WS_CHILD|WS_VISIBLE,	1001},
		{_T("Button"),		_T("Popup-up Wnd"),		WS_CHILD|WS_VISIBLE,	1002},
		{_T("Button"),		_T("Child Wnd"),		WS_CHILD|WS_VISIBLE,	1003},
		{_T("Button"),		_T("Layyer Wnd"),		WS_CHILD|WS_VISIBLE,	1004},
		{_T("Button"),		_T("Message Wnd"),		WS_CHILD|WS_VISIBLE,	1005},
	};

	for (int ii = 0; ii < m_nCtrlCount; ii++)
	{
		int x = rtClient.left + (nBtnWidth + nInterval) * ii;
		int y = rtClient.top;
		hCtrls[ii] = CreateWindow(CtrlInfoList[ii].m_szClsName, 
								  CtrlInfoList[ii].m_szCaption, 
								  CtrlInfoList[ii].m_dwStyle, 
								  x, 
								  y, 
								  nBtnWidth,
								  nBtnHeight,
								  hWnd,
								  (HMENU)CtrlInfoList[ii].m_nID, 
								  NULL,
								  NULL);
	}

	return true;
}

bool CDemoWnd::_InitCtrls(HWND hWnd, HWND* hCtrls)
{
	

	return true;
}

void CDemoWnd::_DestroyCtrls(HWND* hCtrls)
{
	for (int ii = 0; ii < m_nCtrlCount; ii++)
	{
		if (NULL != hCtrls[ii] && IsWindow(hCtrls[ii]))
		{
			CloseWindow(hCtrls[ii]);
		}
	}
}